{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6cc7017f-dcd8-47cf-9642-12ba061d8cf8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-09-09 14:16:57.301106: I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.\n",
      "2025-09-09 14:16:57.307902: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n",
      "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n",
      "E0000 00:00:1757398617.314742  697011 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n",
      "E0000 00:00:1757398617.316717  697011 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n",
      "W0000 00:00:1757398617.323323  697011 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n",
      "W0000 00:00:1757398617.323335  697011 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n",
      "W0000 00:00:1757398617.323336  697011 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n",
      "W0000 00:00:1757398617.323337  697011 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n",
      "2025-09-09 14:16:57.326206: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
      "To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "model_name = './gpt2'\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)\n",
    "model = AutoModelForCausalLM.from_pretrained(model_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9321e972-7376-4656-b6a6-e21ca1aadcf2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "124439808"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_params = sum(p.numel() for p in model.parameters())\n",
    "\n",
    "num_params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1aa8621f-ec4b-415c-a658-75048aa6d368",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"Hello, this is the first step of RLHF training.\"\n",
    "tokens = tokenizer(text)\n",
    "print(tokens)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4bc4c9d-78eb-4d65-a2cd-65de8164e617",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(tokenizer.decode(tokens['input_ids']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9c6738d4-7b34-4132-91a8-4af3ad3e8c9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "texts = ['Hello, this is the first step of RLHF training.', 'I have a dog', 'I also have a cat']\n",
    "tokens_obj = tokenizer(texts)\n",
    "\n",
    "for tokens in tokens_obj['input_ids']:\n",
    "    print(tokenizer.decode(tokens))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a11b868-262d-46f8-8b87-76bbe9dec84d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline, set_seed\n",
    "from pprint import pprint\n",
    "g = pipeline('text-generation', model='./gpt2')\n",
    "set_seed(42)\n",
    "pprint(g(\"this is a movie that\", max_length=30, num_return_sequences=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1946f675-a388-480b-8dfe-77bdc32532d1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
